package com.yixun.controller;

import com.yixun.entity.YxOperate;
import com.yixun.entity.YxUser;
import com.yixun.service.OptService;
import com.yixun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/operate")
public class OperateController extends BaseController {

    @Autowired
    private OptService optService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/list")
    public String list(){
        return "org/operate/operateList";
    }

    /**
     * 获取左侧权限树
     */
    @RequestMapping(value = "/getOperateTreeJsonStr/{optId}")
    @ResponseBody
    public String getOperateTreeJsonStr(@PathVariable("optId") String optId){
        //System.out.println("=============" + optService.getOperateTree(optId));
        return optService.getOperateTree(optId);
    }

    /**
     * 获取子权限
     */
    @RequestMapping(value = "/getChildOperateList/{optId}")
    @ResponseBody
    public List<YxOperate> getChildOperateList(@PathVariable("optId") String optId){
        return optService.getChildOptList(optId);
    }

    @RequestMapping(value = "/go_operate_add")
    public String goOperateAdd(String parentID, Model model){
        model.addAttribute("parentID",parentID);
        model.addAttribute("opt_id","null");
        return "org/operate/operate_add";
    }

    @RequestMapping(value = "/go_operate_update")
    public String goOperateUpdate(String parentID, String opt_id, Model model){
        model.addAttribute("parentID",parentID);
        model.addAttribute("opt_id",opt_id);
        return "org/operate/operate_add";
    }

    /**
     * 添加operate
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public boolean add(@RequestBody YxOperate operate){
        Map<String, Object> session = userService.getUserBySession(this.getSession());
        YxUser sessionUser = (YxUser) session.get("data");
        int row = optService.insertOpt(sessionUser, operate);
        return row == 1;
    }

    /**
     * 根据opt_id获取operate
     */
    @RequestMapping(value = "/getOperateBean/{opt_id}")
    @ResponseBody
    public YxOperate getOperateBean(@PathVariable(value = "opt_id") String opt_id){
        return optService.getOperateBean(opt_id);
    }

    /**
     * 修改operate
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public boolean edit(@RequestBody YxOperate operate){
        Map<String, Object> session = userService.getUserBySession(this.getSession());
        YxUser sessionUser = (YxUser) session.get("data");
        int row = optService.updateOpt(sessionUser, operate);
        return row == 1;
    }

    /**
     * 删除operate
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public boolean del(String id){
        Map<String, Object> session = userService.getUserBySession(this.getSession());
        YxUser sessionUser = (YxUser) session.get("data");
        int row = optService.delOpt(sessionUser,id);
        return row > 0;
    }

    /**
     * 跳向select_singl_operate
     */
    @RequestMapping(value = "/select_singl_operate")
    public String selectSinglOperate(String handl_name, Model model){
        model.addAttribute("handl_name",handl_name);
        return "org/operate/select_singl_operate";
    }

    /**
     * 移动operate
     */
    @RequestMapping(value = "/move")
    @ResponseBody
    public boolean move(String parent_id, String opt_ids){
        Map<String, Object> session = userService.getUserBySession(this.getSession());
        YxUser sessionUser = (YxUser) session.get("data");
        int row = optService.moveOpt(sessionUser, parent_id, opt_ids);
        return row > 0;
    }

    /**
     * 排序 operate
     */
    @RequestMapping(value = "/sort")
    @ResponseBody
    public boolean sort(String id){
        Map<String, Object> session = userService.getUserBySession(this.getSession());
        YxUser sessionUser = (YxUser) session.get("data");
        int row = optService.sortOpt(sessionUser, id);
        return row > 0;
    }

    @RequestMapping(value = "/go_select_operate")
    public String go_select_operate(String handle_name, String update_status, Model model){
        model.addAttribute("handle_name", handle_name);
        model.addAttribute("update_status", update_status);
        return "org/operate/select_operate";
    }
}
